package com.woniushop.dao.imple;

import java.util.List;

import com.woniushop.dao.BaseDAO;
import com.woniushop.dao.OrderInfoDAO;
import com.woniushop.entity.OrderInfo;

/**
 * 订单明细DAO的实现类
 * 
 * @author 崔新雄
 *
 */
public class OrderInfoDAOImple implements OrderInfoDAO {

	private BaseDAO<OrderInfo> baseDAO = new BaseDAO<OrderInfo>();

	/**
	 * 将orderInfo对象加入数据库中
	 * 
	 * @param orderInfo
	 */
	@Override
	public void add(OrderInfo orderInfo) {
		String sql = "insert into orderinfo(oid,gid,oiprice,oinum) values(?,?,?,?)";
		Object[] objs = { orderInfo.getOid(), orderInfo.getGid(), orderInfo.getOiprice(), orderInfo.getOinum() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据订单明细id删除数据库中对应的订单明细数据
	 * 
	 * @param oiid
	 */
	@Override
	public void delete(Integer oiid) {
		String sql = "delete from orderinfo where oiid=?";
		Object[] objs = { oiid };
		baseDAO.update(sql, objs);
	}

	/**
	 * 修改对应的订单明细数据
	 * 
	 * @param orderInfo
	 */
	@Override
	public void update(OrderInfo orderInfo) {
		String sql = "update orderinfo set oid=?,gid=?,oiprice=?,oinum=? where oiid=?";
		Object[] objs = { orderInfo.getOid(), orderInfo.getGid(), orderInfo.getOiprice(), orderInfo.getOinum(),
				orderInfo.getOiid() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据对应的订单明细id查找订单明细数据
	 * 
	 * @param oid
	 * @return
	 */
	@Override
	public OrderInfo findOne(Integer oiid) {
		String sql = "select oi.*,o.ono,o.odate,g.gname,g.gpic,g.gprice,g.gstock from orderinfo oi,orderform o,goods g where oi.oid=o.oid and oi.gid=g.gid and oiid=?";
		Object[] objs = { oiid };
		List<OrderInfo> list = baseDAO.select(sql, OrderInfo.class, objs);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据对应的订单id和商品id查找订单明细数据
	 * 
	 * @param oid
	 * @return
	 */
	@Override
	public OrderInfo findOne(Integer oid, Integer gid) {
		String sql = "select oi.*,o.ono,o.odate,g.gname,g.gpic,g.gprice,g.gstock from orderinfo oi,orderform o,goods g where oi.oid=o.oid and oi.gid=g.gid and oi.oid=? and oi.gid=?";
		Object[] objs = { oid, gid };
		List<OrderInfo> list = baseDAO.select(sql, OrderInfo.class, objs);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 多条件分页查询订单明细数据
	 * 
	 * @param page
	 * @param pageSize
	 * @param orderInfo
	 * @return
	 */
	@Override
	public List<OrderInfo> findByPage(Integer page, Integer pageSize, OrderInfo orderInfo) {
		String sql = "select oi.*,o.ono,o.odate,g.gname,g.gpic,g.gprice,g.gstock from orderinfo oi,orderform o,goods g where oi.oid=o.oid and oi.gid=g.gid";
		sql = finishSQL(orderInfo, sql);
		sql += " limit ?,?";
		return baseDAO.select(sql, OrderInfo.class, (page - 1) * pageSize, pageSize);
	}

	/**
	 * 查找符合条件的订单明细数量
	 * 
	 * @param orderInfo
	 * @return
	 */
	@Override
	public Integer count(OrderInfo orderInfo) {
		String sql = "select count(*) from orderinfo oi,orderform o,goods g where oi.oid=o.oid and oi.gid=g.gid";
		sql = finishSQL(orderInfo, sql);
		return baseDAO.count(sql);
	}

	/**
	 * 完成多条件查询的SQl语句
	 * 
	 * @param orderInfo
	 * @param sql
	 * @return
	 */
	private String finishSQL(OrderInfo orderInfo, String sql) {
		Integer gid = orderInfo.getGid();
		if (gid != null) {
			sql += " and  gid='" + gid + "'";
		}
		Integer oid = orderInfo.getOid();
		if (oid != null) {
			sql += " and  oi.oid='" + oid + "'";
		}
		return sql;
	}

	/**
	 * 根据订单明细id批量删除数据库中对应的订单明细数据
	 * 
	 * @param oiid
	 */
	@Override
	public void deleteAll(Integer[] oiids) {
		String sql = "delete from orderinfo where oiid in (";
		for (int i = 0; i < oiids.length; i++) {
			if (i == oiids.length - 1) {
				sql += oiids[i] + ")";
			} else {
				sql += oiids[i] + ",";
			}
		}
		baseDAO.update(sql);
	}

	@Override
	public void deleteByOid(Integer oid) {
		String sql = "delete from orderinfo where oid=?";
		Object[] objs = { oid };
		baseDAO.update(sql, objs);
	}

}
